package gold.contest;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5716 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 竞赛hard难度不愧是hard！
     * @return:
     */
    class Solution {
        public int maxNiceDivisors(int n) {
            int N = 10_0000_0007;
            if (n <= 3) {
                return n;
            }
            int a = n / 3, b = n % 3;
            if (b == 1) {
                return (int) (quickPow(3, a - 1, N) * 4 % N);
            } else if (b == 2) {
                return (int) (quickPow(3, a, N) * 2 % N);
            } else {
                return (int) quickPow(3, a, N);
            }
        }

        /**
         * 快速求幂：
         * p^q，计算中防止溢出，对MOD求余
         */
        public long quickPow(int p, int q, int MOD) {
            long ans = 1L;
            long base = p;
            while (q != 0) {
                if ((q & 1) == 1) {
                    ans = ans * base % MOD;
                }
                base = base * base % MOD;
                q >>>= 1;
            }
            return ans;
        }
    }


    class Solution_Fail {

        Map<Integer, Long> memo = new HashMap<Integer, Long>();
        int factor = 1000000007;

        public int maxNiceDivisors(int n) {
            if (n <= 3) return n;
            if (n > 1000) n = 1000;
            return (int) (integerBreak(n).intValue() % factor);
        }

        public Long integerBreakRecur(int n) {
            if (n <= 3) return ((long) n);
            if (memo.containsKey(n)) return memo.get(n);

            long curMax = Long.MIN_VALUE;
            for (int i = 2; i < n; i++) {
                long left = integerBreakRecur(i);
                long right = integerBreakRecur(n - i);
                long cur = left * right;
                if (curMax < cur) curMax = cur;
            }

            memo.put(n, curMax);
            return curMax;
        }

        //动态规划
        public BigDecimal integerBreak(int n) {
            BigDecimal[] dp = new BigDecimal[n + 1];//dp[i]代表当前i的最大乘积
            dp[1] = new BigDecimal(1);
            dp[2] = new BigDecimal(1);
            //得出每个i的最大乘积
            for (int i = 3; i <= n; i++) {
                //每个i都可分成1+(i-1),2+(i-2)......
                for (int j = 1; j <= i - 1; j++) {
                    //比较每中分法的大小
                    dp[i] = new BigDecimal(Math.max(dp[i].intValue(), Math.max(j * (i - j), j * dp[i - j].intValue())));
                }
            }
            return dp[n];
        }
    }


    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.maxNiceDivisors(545918790));
    }

    public static void main(String[] args) throws Exception {
        LC5716 an = new LC5716();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
